import express from 'express'

import { datasource } from '../db/index.js'
import permissionEntity from '../model/permissionModel.js'

const router = express.Router()

const permissionRepository = datasource.getRepository(permissionEntity)

router.post('/create', async (req, res) => {
  let { name, desc } = req.body

  name = name ? name.trim() : null

  desc = desc ? desc.trim() : null

  if (!name) {
    res.json({
      code: -1,
      data: null,
      message: '请输入权限名称'
    })
    return
  }

  let exitPermission = await permissionRepository.findOne({
    where: {
      name
    }
  })

  if (exitPermission) {
    res.json({
      code: -1,
      data: null,
      message: '权限名称已存在'
    })
    return
  }

  const result = await permissionRepository.save({
    name,
    desc
  })

  console.log(result)

  res.json({
    code: 1000,
    data: null,
    messaeg: '操作成功'
  })
})

router.post('/update', async (req, res) => {})

router.post('/list', async (req, res) => {})

router.post('/delete', async (req, res) => {})

export default router
